//package org.test.jiazi_1.common.utils;
//
//import cn.idev.excel.EasyExcel;
//import jakarta.servlet.http.HttpServletResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.math3.genetics.Fitness;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.test.jiazi_1.common.constant.MessageConstant;
//import org.test.jiazi_1.pojo.entity.Student;
//import org.test.jiazi_1.service.StudentService;
//
//import java.io.IOException;
//import java.net.URLEncoder;
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//import java.util.Objects;
//import java.util.function.BiConsumer;
//import java.util.function.Function;
//import java.util.stream.Stream;
//
//
//
//@Slf4j
//@Service
//public class CommonService {
//
//    @Autowired
//    private StudentService studentBaseService;
//
//
//    public static void calculateAndSetGenderBasedScores(Score Score, Fitness f, boolean male, boolean oneTwo,
//                                                        boolean isLimit) {
//        // 定义一个函数来处理分数计算和设置
//        Function<Double, Double> scoreProcessor = isLimit ? CommonService::limitScore : score -> score;
//        if (male) {
//            // 一千米
//            Score.setOneThousandMeterRunScore(scoreProcessor.apply(ScoreCalculator.calculateOneThousandMeterRunScore(f.getOneThousandMeterRun(), oneTwo)));
//            // 引体向上
//            Score.setPullUpsScore(scoreProcessor.apply(ScoreCalculator.calculatePullUpsScore(f.getPullUps(), oneTwo)));
//
//        } else {
//            // 800米
//            Score.setEightHundredMeterRunScore(scoreProcessor.apply(ScoreCalculator.calculateEightHundredMeterRunScore(f.getEightHundredMeterRun(), oneTwo)));
//            // 一分钟仰卧起坐
//            Score.setOneMinuteSitUpsScore(scoreProcessor.apply(ScoreCalculator.calculateOneMinuteSitUpsScore(f.getOneMinuteSitUps(), oneTwo)));
//        }
//    }
//
//    public static Double limitScore(Double score) {
//        return score < 100 ? score : 100;
//    }
//
//
//    private static void setLevelIfScoreExists(ExcelDTO excelDTO,
//                                              Double score, BiConsumer<ExcelDTO, String> setter) {
//        if (score != null) {
//            setter.accept(excelDTO, getLevel(score));
//        }
//    }
//
//    public static void setLevels(ExcelDTO excelDTO) {
//        setLevelIfScoreExists(excelDTO, excelDTO.getWeightScore(),
//                ExcelDTO::setWeightLevel);
//        setLevelIfScoreExists(excelDTO, excelDTO.getLungCapacityScore(),
//                ExcelDTO::setLungCapacityLevel);
//        setLevelIfScoreExists(excelDTO, excelDTO.getFiftyMeterRunScore(),
//                ExcelDTO::setFiftyMeterRunLevel);
//        setLevelIfScoreExists(excelDTO, excelDTO.getStandingLongJumpScore(),
//                ExcelDTO::setStandingLongJumpLevel);
//        setLevelIfScoreExists(excelDTO, excelDTO.getSitAndReachScore(),
//                ExcelDTO::setSitAndReachLevel);
//        setLevelIfScoreExists(excelDTO, excelDTO.getOneThousandMeterRunScore(),
//                ExcelDTO::setOneThousandMeterRunLevel);
//        setLevelIfScoreExists(excelDTO, excelDTO.getPullUpsScore(),
//                ExcelDTO::setPullUpsLevel);
//        setLevelIfScoreExists(excelDTO, excelDTO.getEightHundredMeterRunScore(),
//                ExcelDTO::setEightHundredMeterRunLevel);
//        setLevelIfScoreExists(excelDTO, excelDTO.getOneMinuteSitUpsScore(),
//                ExcelDTO::setOneMinuteSitUpsLevel);
//        setLevelIfScoreExists(excelDTO, excelDTO.getTotalScore(),
//                ExcelDTO::setTotalScoreLevel);
//    }
//
//    // 分数数据复制
//    public static void copyScoreData(FitnessTestScores scores) {
//        scores.setWeightScore(scores.getWeightScore());
//        scores.setVitalCapacityScore(scores.getVitalCapacity());
//        scores.setRun50mScore(scores.getFiftyMeterRunScore());
//        scores.setLongJumpScore(scores.getStandingLongJumpScore());
//        scores.setSitAndReachScore(scores.getSitAndReachScore());
//        scores.setRun800mScore(scores.getEightHundredMeterRunScore());
//        scores.setRun1000mScore(scores.getOneThousandMeterRunScore());
//        scores.setPullUpsBonus(scores.getOneMinuteSitUpsScore());
//        scores.setSitUpsScore(scores.getPullUpsScore());
//        //没写错，这里是没有加附加分
//        scores.setStandardScore(scores.getTotalScore());
//    }
//
//    // 学生基础信息复制
//    public void copyBasicInfo(ExcelDTO dto, Fitness f, Student student, String gradeNumber) {
//        dto.setGradeNumber(gradeNumber);
//        dto.setClassId(student.getClassName());
//        dto.setClassName(student.getClassName());
//        dto.setId(student.getId());
//        //去掉ethnicCode前面的0，如01变1
//        dto.setEthnicCode(student.getEthnicCode().replaceFirst("^0+", ""));
//        dto.setName(student.getName());
//        dto.setSex(getSex(student.getSex()));
//        dto.setBirthday(student.getBirthday());
//    }
//
//
//    // 数据复制
//    public static void copyMeasurementData(ExcelDTO dto, Fitness f) {
//        BeanUtils.copyProperties(f, dto);
//        dto.setEightHundredMeterRun(getTime(f.getEightHundredMeterRun()));
//        dto.setOneThousandMeterRun(getTime(f.getOneThousandMeterRun()));
//    }
//
//    // DTO转换方法
//    public ExcelDTO convertToXlsxDTO(Fitness f, Student student, Score score,Integer state) {
//        String gradeNumber = fitnessRepository.getGradeNumberById(f.getId());
//        ExcelDTO dto = new ExcelDTO();
//        // 学生基础信息
//        copyBasicInfo(dto, f, student, gradeNumber);
//        // 体测数据复制
//        copyMeasurementData(dto, f);
//        // 分数数据
//        copyScoreData(dto, score);
//        // 附加分处理
//        processAdditionalScores(dto, score, student, f, gradeNumber);
//        // 等级处理
//        processLevels(dto);
//        //备注处理
//        state(dto,state);
//        return dto;
//    }
//
//    private void state(ExcelDTO dto, Integer state) {
//        String s = "";
//        if(state==1) {
//            s = "免测";
//        } else if (state==2) {
//            s = "退伍";
//        }
//        dto.setState(s);
//    }
//
//    // 附加分处理
//    public static void processAdditionalScores( FitnessTestScores scores, Student student,  String gradeNumber) {
//
//        boolean male = student.getSex() == 1;
//        boolean isOneTwo = gradeNumber.equals("41") || gradeNumber.equals("42");
//
//        // 处理男性和女性附加分
//        //先将限制取消--可以超过100分
//        calculateAndSetGenderBasedScores(Score, f, male, isOneTwo, false);
//        //计算附加分--将超过100分的作为附加分
//        setAdditional(Score, dto, male);
//        //再将限制恢复
//        calculateAndSetGenderBasedScores(Score, f, male, isOneTwo, true);
//    }
//
//    // 等级处理
//    public static void processLevels(ExcelDTO xlsxDTO) {
//        //总附加分
//        xlsxDTO.setExtraScore(calculateTotalExtraScore(xlsxDTO));
//        // 总分
//        xlsxDTO.setTotalScore(xlsxDTO.getExtraScore() + xlsxDTO.getStandardScore());
//        // 设置等级
//        setLevels(xlsxDTO);
//        // BMI计算
//        updateBMILevels(xlsxDTO);
//    }
//
//    // 附加分汇总
//    public static Integer calculateTotalExtraScore(ExcelDTO dto) {
//        return Stream.of(
//                dto.getPullUpsExtraScore(),
//                dto.getOneThousandMeterRunExtraScore(),
//                dto.getEightHundredMeterRunExtraScore(),
//                dto.getOneMinuteSitUpsExtraScore()
//        ).filter(Objects::nonNull).mapToInt(Integer::intValue).sum();
//    }
//
//
//    private static void setAdditional(Score Score, ExcelDTO xlsxDTO, boolean isMale) {
//        // 处理男性专属附加分
//        if (isMale) {
//            setExtraScoreIfExceed(Score.getOneThousandMeterRunScore(),
//                    xlsxDTO::setOneThousandMeterRunExtraScore);
//            setExtraScoreIfExceed(Score.getPullUpsScore(),
//                    xlsxDTO::setPullUpsExtraScore);
//        }
//        // 处理女性专属附加分
//        else {
//            setExtraScoreIfExceed(Score.getEightHundredMeterRunScore(),
//                    xlsxDTO::setEightHundredMeterRunExtraScore);
//            setExtraScoreIfExceed(Score.getOneMinuteSitUpsScore(),
//                    xlsxDTO::setOneMinuteSitUpsExtraScore);
//        }
//    }
//
//
//    // BMI等级计算简化
//    public static void updateBMILevels(ExcelDTO dto) {
//        if (dto.getWeight() == null || dto.getHeight() == null) {
//            return;
//        }
//        double bmi = dto.getWeight() / (dto.getHeight() * dto.getHeight() / ONE_MILLION);
//        boolean isMale = "男".equals(dto.getSex());
//        double underweightThreshold = isMale ? UNDERWEIGHT_BMI_MAN : UNDERWEIGHT_BMI_WOMAN;
//
//        if (bmi >= OBESE_BMI) {
//            dto.setWeightLevel(OBESE_L);
//        } else if (bmi >= OVERWEIGHT_BMI) {
//            dto.setWeightLevel(OVERWEIGHT_L);
//        } else if (bmi < underweightThreshold) {
//            dto.setWeightLevel(UNDERWEIGHT_L);
//        } else {
//            dto.setWeightLevel(NORMAL_L);
//        }
//        // 特例处理
//        if (NORMAL_L.equals(dto.getWeightLevel()) && dto.getWeightScore() == 80) {
//            dto.setWeightLevel(UNDERWEIGHT_L);
//        }
//    }
//
//
//    public static void calculateScores(Score Score, Fitness f, boolean male, boolean oneTwo) {
//        // 使用ScoreCalculator计算成绩
//        Score.setLungCapacityScore(ScoreCalculator.calculateLungCapacityScore(f.getLungCapacity(), male, oneTwo));
//        Score.setFiftyMeterRunScore(ScoreCalculator.calculateFiftyMeterRunScore(f.getFiftyMeterRun(), male, oneTwo));
//        Score.setStandingLongJumpScore(ScoreCalculator.calculateStandingLongJumpScore(f.getStandingLongJump(), male,
//                oneTwo));
//        Score.setSitAndReachScore(ScoreCalculator.calculateSitAndReachScore(f.getSitAndReach(), male, oneTwo));
//        Score.setWeightScore(ScoreCalculator.calculateBMIScore(f.getHeight(), f.getWeight(), male));
//        //男女--特殊成绩
//        calculateAndSetGenderBasedScores(Score, f, male, oneTwo, true);
//    }
//
//
//    public static double getTotalScore(Score Score, boolean male) {
//        double totalScore = Score.getLungCapacityScore() * LUNG_CAPACITY_WEIGHT +
//                Score.getFiftyMeterRunScore() * FIFTY_METER_RUN_WEIGHT +
//                Score.getStandingLongJumpScore() * STANDING_LONG_JUMP_WEIGHT +
//                Score.getSitAndReachScore() * SIT_AND_REACH_WEIGHT +
//                Score.getWeightScore() * BMI_WEIGHT;
//
//        totalScore = getTotalForMale(Score, male, totalScore) / 100;
//
//        return totalScore;
//    }
//
//    private static double getTotalForMale(Score Score, boolean male, double totalScore) {
//        if (male) {
//            totalScore += (Score.getOneThousandMeterRunScore() * EIGHT_HUNDRED_METER_RUN_OR_ONE_THOUSAND_METER_RUN_WEIGHT) +
//                    (Score.getPullUpsScore() * PULL_UPS_OR_ONE_MINUTE_SIT_UPS_WEIGHT);
//        } else {
//            totalScore += (Score.getEightHundredMeterRunScore() * EIGHT_HUNDRED_METER_RUN_OR_ONE_THOUSAND_METER_RUN_WEIGHT) +
//                    (Score.getOneMinuteSitUpsScore() * PULL_UPS_OR_ONE_MINUTE_SIT_UPS_WEIGHT);
//        }
//        return totalScore;
//    }
//
//
//
//}